import requests
from lxml import etree
from time import sleep

url = "https://www.chinabidding.com/search/proj.htm"


class Spider(object):

    def __init__(self):
        self.payload = {
            'fullText': '上海华力',
            'pubDate': '',
            'infoClassCodes': '0105',
            'normIndustry': '',
            'zoneCode': '',
            'fundSourceCodes': '',
            'poClass': 'BidNotice',
            'rangeType': '',
            'currentPage': ''
        }
        self.headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36',
            'referer': 'https://www.chinabidding.com/search/proj.htm',
            'cookie': '__yjs_duid=1_f0abdf858ae3dfaf850adc425b857a641647931522280; JSESSIONID=50FB608C070BCAEEE5CC8DBDEED2EC82; Hm_lvt_3e8bc71035a13d1213b8be00baf17f95=1647931525,1647931678,1647931698,1647932340; Hm_lpvt_3e8bc71035a13d1213b8be00baf17f95=1647932551',
            'Content-Type': 'application/x-www-form-urlencoded'

        }

    def get_data_one(self, url):
        response = requests.post(url=url, headers=self.headers, data=self.payload)
        if response.status_code == 200:
            return response.text

    def parse_main_page(self):
        for page in range(1, 100):
            print(f'----------------正在下载第{page}页---------------------')
            self.payload['currentPage'] = page
            html_one_text = etree.HTML(self.get_data_one(url))
            l_properties = html_one_text.xpath('//*[@id="lab-show"]/div[2]/div[2]/div/ul/li')
            No_item = ['建筑工程', '其他服务', '物流服务', '其它工程;建筑工程']
            for result in l_properties:
                label = ''.join(result.xpath('a/div/dl/dd/span[1]/strong/text()'))
                title_n = ''.join(result.xpath('a/h5/span/@title')).split('公告')[1].split('(')[1].split(')')[0]
                if label in No_item:
                    print('看这里')
                elif title_n == '2':
                    print('重复项')
                    pass
                else:
                    href = ''.join(result.xpath('a/@href'))
                    # print(href)
                    self.parse_data(href)

    def parse_data(self, href):
        html_text = etree.HTML(self.get_data_one(href))
        all_text = html_text.xpath('//*[@id="lab-show"]//text()')
        title = html_text.xpath('//*[@id="lab-show"]/div[2]/div[1]/div/h3/text()')  # 标题
        time = html_text.xpath('//*[@id="lab-show"]/div[2]/div[1]/div/p/span/text()')  # 招标时间
        text = ''.join(all_text)
        Item_No = text.split('招标项目编号:')[1].split('招标项目名称')[0]  # 项目编号
        # print(text)
        Project_ov = text.split('项目概况:')[1].split('资金到位')[0]  # 项目概况
        Entry_name = text.split('项目名称:')[1].split('实施地点:')[0]  # 项目名称
        Project_il_adress = text.split('项目实施地点:')[1].split('招标产品列表')[0]  # 项目实施地点

        print(Item_No, Project_ov, Entry_name, Project_il_adress)

        print('=======' * 20)
        # print(html_text)


if __name__ == '__main__':
    a = Spider()
    a.parse_main_page()
